package org.example.main.service.impl;

import org.apache.log4j.Logger;
import org.example.main.dao.RentalApplicationMapper;
import org.example.main.entity.RentalApplication;
import org.example.main.service.RentalApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RentalApplicationServiceImpl implements RentalApplicationService {

    private static final Logger logger = Logger.getLogger(RentalApplicationServiceImpl.class);

    @Autowired
    private RentalApplicationMapper rentalApplicationMapper;

    @Override
    public int apply(RentalApplication application) {
        logger.info("Applying for rental - tenant ID: " + application.getTenantId() + ", house ID: " + application.getHouseId());
        int result = rentalApplicationMapper.insert(application);
        logger.info("Rental application added with ID: " + application.getApplicationId() + ", result: " + result);
        return result;
    }

    @Override
    public int updateStatus(long applicationId, String status) {
        logger.info("Updating rental application status - application ID: " + applicationId + ", status: " + status);
        int result = rentalApplicationMapper.updateStatus(applicationId, status);
        logger.info("Rental application status update result: " + result);
        return result;
    }

    @Override
    public RentalApplication findById(long applicationId) {
        logger.info("Finding rental application by ID: " + applicationId);
        RentalApplication application = rentalApplicationMapper.findById(applicationId);
        logger.debug("Found rental application: " + application);
        return application;
    }

    @Override
    public List<RentalApplication> findByTenantId(long tenantId) {
        logger.info("Finding rental applications by tenant ID: " + tenantId);
        List<RentalApplication> applications = rentalApplicationMapper.findByTenantId(tenantId);
        logger.debug("Found " + applications.size() + " rental applications for tenant ID: " + tenantId);
        return applications;
    }

    @Override
    public List<RentalApplication> findByHouseId(long houseId) {
        logger.info("Finding rental applications by house ID: " + houseId);
        List<RentalApplication> applications = rentalApplicationMapper.findByHouseId(houseId);
        logger.debug("Found " + applications.size() + " rental applications for house ID: " + houseId);
        return applications;
    }

    @Override
    public RentalApplication findByTenantIdAndHouseIdAndType(long tenantId, long houseId, String type){
        logger.info("Finding rental application by tenant ID: " + tenantId + ", house ID: " + houseId + ", type: " + type);
        RentalApplication application = rentalApplicationMapper.findByTenantIdAndHouseIdAndType(tenantId, houseId, type);
        logger.debug("Found rental application: " + application);
        return application;
    }

    @Override
    public List<RentalApplication> findAll() {
        logger.info("Retrieving all rental applications");
        List<RentalApplication> applications = rentalApplicationMapper.findAll();
        logger.debug("Found " + applications.size() + " rental applications");
        return applications;
    }

    @Override
    public int delete(long applicationId){
        logger.info("Deleting rental application by ID: " + applicationId);
        int result = rentalApplicationMapper.deleteById(applicationId);
        logger.info("Rental application deletion result: " + result);
        return result;
    }
}
